package tmall.domain.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tmall.common.config.AppConst;
import tmall.common.controller.BaseController;
import tmall.common.dto.PageDTO;
import tmall.common.dto.ResponseResult;
import tmall.common.util.ApiRet;
import tmall.common.vo.PageVO;
import tmall.domain.entity.Goods;
import tmall.domain.service.GoodsService;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author zhangservlet
 * @date 2020/11/30
 */
@Api("商品详情")
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController {

    @Resource
    private GoodsService goodsService;

    @Value("${image.path}")
    private String imgPath;

    @GetMapping("/getAll")
    @ApiOperation(value = "获取所有商品类目", httpMethod = "GET")
    public ResponseResult getAll(String keyWord) throws Exception {
        logger.info("请求接口: /goods/getAll");
        Map parmas = new HashMap();
        parmas.put("keyWord", keyWord);
        List list = goodsService.findAllBy(parmas);
        logger.info("数据list = {}", list);
        return ApiRet.success(list);
    }

    @PostMapping("/list")
    @ApiOperation(value = "分页查询类目", httpMethod = "POST")
    public ResponseResult list(PageVO pageVO) {
        logger.info("请求接口: /goods/list");
        PageDTO result = goodsService.list(pageVO.getPageNum(), pageVO.getPageSize(), pageVO.getKeyWord());
        logger.info("result = ", result);
        return ApiRet.success(result);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加类目", httpMethod = "POST")
    public ResponseResult add(Goods entity) {
        logger.info("请求接口 : /goods/add");
        goodsService.insert(entity);
        return ApiRet.success("成功");
    }

    @PostMapping("/edit")
    @ApiOperation(value = "编辑类目", httpMethod = "POST")
    public ResponseResult edit(Goods entity) {
        logger.info("请求接口 : /goods/edit");
        goodsService.update(entity);
        return ApiRet.success("成功");
    }

    @ResponseBody
    @PostMapping(value = "/uploadImg")
    @ApiOperation(value = "上传图片", httpMethod = "POST")
    public ResponseResult uploadImg(@RequestParam MultipartFile file) throws IOException {

        logger.info("请求接口 : /goods/uploadImg");
        String originalFileName = file.getOriginalFilename();
        logger.info("获取图片原始文件名: {}", originalFileName);
        String extension = originalFileName.substring(originalFileName.lastIndexOf('.'));
        String fileName = UUID.randomUUID() + extension;
        String filePath = System.getProperty("user.dir") + imgPath + AppConst.GOODS_IMG_FOLDER + fileName;

        super.uploadImg(filePath, file);

        return ApiRet.success(fileName);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除类目", httpMethod = "GET")
    public ResponseResult delete(Long id) throws Exception {
        logger.info("请求接口: /goods/delete");
        int ret = goodsService.delete(id);
        logger.info("生效行数 = {}", ret);
        return ApiRet.success("成功");
    }

}
